/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.bank.wx.helper;

import com.alibaba.fastjson.JSON;
import com.synjones.pay.bank.wx.bean.common.ExtendedServApiUrl;
import com.synjones.pay.bank.wx.bean.common.WxResponseBean;
import com.synjones.pay.bank.wx.bean.idinfo.YktIdInfoBaseRespBean;
import com.synjones.pay.model.context.StatusInfo;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liubinqiang
 * @date 2017-5-20 13:58:35
 * @version V1.0
 * @desc
 */
public class ExtendedServHelper {

    private static Logger LOGGER = LoggerFactory.getLogger(ExtendedServHelper.class);
    private static final String EXTENDED_URL = "localhost:8050";

    public static String getDeptBySno(String sno) {
        YktIdInfoBaseRespBean idInfoBean = getIdInfo(sno);
        if (idInfoBean != null) {
            return idInfoBean.getIdinfo().getDept();
        }
        return "";
    }

    public static YktIdInfoBaseRespBean getIdInfo(String sno) {
        String result = postToServer(ExtendedServApiUrl.ID_INFO, sno);
        WxResponseBean wxResponseBean = JSON.parseObject(result, WxResponseBean.class);
        if (wxResponseBean != null && wxResponseBean.getErrCode().equals("0")) {
            YktIdInfoBaseRespBean respBean = JSON.parseObject(wxResponseBean.getData().toString(), YktIdInfoBaseRespBean.class);
            if (respBean != null && respBean.getErrCode().equals("0")) {
                return respBean;
            }
        }
        return null;
    }

    public static String postToServer(String urlString, String postData) {
//        String urlString = BASE_URL + WxApiStr.TRANSFER_URL;
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
//            connection.setRequestProperty("Accept-Charset", "utf-8");
//            connection.setRequestProperty("contentType", "utf-8");
            connection.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
//            postData = URLEncoder.encode(postData, "utf-8");
            wr.writeBytes(postData);
            wr.flush();
            wr.close();
            int responseCode = connection.getResponseCode();
            LOGGER.info("postToServer:responseCode=" + responseCode);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "utf-8"));
            String output;
            StringBuffer response = new StringBuffer();
            while ((output = in.readLine()) != null) {
                response.append(output);
            }
            in.close();

            return response.toString();
        } catch (IOException ex) {
            LOGGER.error("postToServer:ex=" + ex.getMessage());
            return null;
        }
    }

    public static WxResponseBean commonResponseBean(WxResponseBean responseBean) {
        if (responseBean == null) {
            return WxResponseBean.genNoData();
        } else {
            if (responseBean.getErrCode().equals("0")) {
                responseBean.setErrCode(StatusInfo._0000.code());
            }
        }
        return responseBean;
    }
}
